Temperaturecontrol

# Temperaturecontrol

This module reads temperature reading sensors ( thermistors ) and uses heater and cooler elements to maintain a set temperature.

This is used for example for extruder hotends, or heated beds.

## Multiple modules

In Smoothie you do not get just one TemperatureControl module. You can actually create as many as you want, simply by adding them to the configuration file.

It goes something like this :

    temperature_control.hotend.enable                    true
    temperature_control.hotend.thermistor_pin            0.23
    etc ...

    temperature_control.bed.enable                       true
    temperature_control.bed.thermistor_pin               0.24
    etc ...

This will create and configure two separate TemperatureControl modules that will act completely independently from each other.

The line that effectively "creates" the module is the enable option. If set to true, a module is created and further configuration is read. If set to false, further configuration for this module is ignored as no module is created.

In the Configuration section bellow, the first two parts ( `temperature_control.module_name` ) of the configuration are sometimes omitted for conciseness, but have to be added in your actual configuration file ( see example ).

## Configuration

### All configuration options

Here are all the possible configuration options for your TemperatureControl modules

Option | Example value | Explanation
--- | --- | ---

temperature_control.module_name.enable | true | Whether to activate this temperaturecontrol module. You can create as many temperaturecontrol modules as you want, simply by giving a new module a name, and setting it's `enable` option to true
temperature_control.module_name.thermistor_pin | `0.23` | Pin for the thermistor to read. ADC ports TH1 to TH4 are pins 0.23 to 0.26.
temperature_control.module_name.readings_per_second | 20 | How many times per second to read temperature from the sensor.
temperature_control.module_name.pwm_frequency | 2000 | How many times per second to switch the heating element on or off. Set to a low value ( 20 ) if using a Solid State Relay.
temperature_control.module_name.heater_pin | `2.7` | Pin that controls the heater. This can be used to control a Mosfet on board or an external Solid State Relay. Set to `nc` if a readonly thermistor is being defined
temperature_control.module_name.thermistor | EPCOS100K | Set the thermistor model for this module. Several different common models are pre-defined, see here for a list
temperature_control.module_name.beta | 4066 | Manually set the `beta` value for your thermistor. This is useful if your thermistor is not in the common pre-defined models.
temperature_control.module_name.r0 | 100000 | Manually set the `r0` resistance value for your thermistor. This is useful if your thermistor is not in the common pre-defined models. Besides `beta` and `r0` which are properties of your thermistor, you can also set the `r1`, `r2` and `t0` values, but those are properties of your board so they usually never have to be changed.
temperature_control.module_name.get_m_code | 105 | Calling this M-code will return the current temperature.
temperature_control.module_name.set_m_code | 104 | This is the M-code for simply setting the temperature. For example here, the value is `104` so you use `M104 S50` to set this module's heater's temperature to 50.
temperature_control.module_name.set_and_wait_m_code | 109 | This is the M-code for setting the temperature then waiting for that temperature to be reached before doing anything. For example here, the value is `109` so you use `M109 S50` to set this module's heater's temperature to 50 and then wait.
temperature_control.module_name.designator | T | The letter this module's temperature will be identified as in the `M105` command's answer. For example here the value is T, so `M105` will answer `ok T:23.4 /0.0 @0`
temperature_control.module_name.p_factor | 13.7 | P factor for PID temperature regulation
temperature_control.module_name.i_factor | 0.097 | I factor for PID temperature regulation
temperature_control.module_name.d_factor | 24 | D factor for PID temperature regulation
temperature_control.module_name.max_pwm | 64 | Maximum PWM value for the heating element. This can be from `0` to `255` . `64` is a good value if driving a 12v resistor with 24v. `255` is the default and the normal value if you are using the right voltage for your heating element.
temperature_control.module_name.bang_bang | false | Set to true to use bang bang control rather than PID
temperature_control.module_name.hysteresis | 2.0 | Set to the temperature in degrees C to use as hysteresis for bang bang control
temperature_control.module_name.i_max | 64 | Maximum value for the I variable in the PID control. This should usually be set to about the same value as `max_pwm` ( as a rule of thumb, it is not actually a pwm setting ). This helps with preventing overshoot when initially heating up. If you get a strong ( >10°C ) overshoot on startup, try setting this to a value lower than `max_pwm`
temperature_control.module_name.sensor | thermistor | Set the type of sensor used to read temperature. Values can be `thermistor` for the usual thermistor reading via ADC method, or `max31855` to read values from a thermocouple over SPI. See Reading a thermocouple
temperature_control.module_name.chip_select_pin | `0.16` | If the sensor is set to `max31855`, sets the chip select pin for the SPI port. This allows you to have multiple sensors sharing the same SPI port, as long as they each get a chip select ( CS ) pin.
temperature_control.module_name.spi_channel | 0 | If the sensor is set to `max31855`, SPI channel using which to talk to the thermocouple chip.
temperature_control.module_name.max_temp | 100 | If set, no temperature above this will be accepted.

### Reading temperatures

To reach a desired temperature, you must be able to know what the current temperature is. This is done using a Thermistor connected to an ADC on the controller board, or a Thermocouple.

#### Thermistor

A given controller board only has a given number of ADCs ( analog ( temperature ) to digital ( Smoothie ) converter ) capable pins.

On the smoothieboard for example, there are 4 thermistor inputs, labelled from T0 to T3, and corresponding in the same order to the pins 0.23 to 0.26. T0 is usually used for the hotend, and T1 for the bed.

Thermistor inputs are not polarized, the direction you connect them in on your board is not important.

    temperature_control.hotend.thermistor_pin        0.23

Smoothieboard thermistor input name | T0 | T1 | T2 | T3
--- | --- | --- | --- | ---
Pin for configuration | 0.23 | 0.24 | 0.25 | 0.26

#### Choosing the right thermistor

There are different models of thermistors. Each hotend or heated bed has one, and it's a specific given type. Different thermistor models translate a given temperature into different resistances. This means you must tell Smoothie which exact thermistor model you have, to be sure Smoothie can read it correctly.

This is done using the thermistor option in the configuration file. You pass it the name of your thermistor, and it will configure the math correctly accordingly.

    temperature_control.hotend.thermistor        EPCOS100K

Smoothie does not know the name of all the thermistors in existence. At the moment these are the ones it knows about:

Name | Beta for 0-80°C | Beta for 185-230°C | I for Steinhart Hart | J for Steinhart Hart | K for Steinhart Hart | Part number
--- | --- | --- | --- | --- | --- | ---
`EPCOS100K` | 4066 | 4193 | 0.000722378300319346F | 0.000216301852054578F | 9.2641025635702e-08F | B57540G0104F000
`Honeywell100K` | 3974 | 4385 | 0.000596153185928425F | 0.000231333192738335F | 6.19534004306738e-08F | 135-104LAG-J01
`Semitec` | 4267 | 4375 | 0.000811290160145459F | 0.000211355789144265F | 7.17614730463848e-08F | 104GT-2
`Honeywell-QAD` |  |  | 0.000827339299500986F | 0.000208786427208899F | 8.05595282332277e-08F | 135-104QAD-J01
`RRRF100K` | 3960 |  |  |  |  |
`RRRF10K` | 3964 |  |  |  |  |
`HT100K` | 3990 |  |  |  |  |

In case you have a thermistor that is not known to Smoothie you can simply define the parameters in config. There are two methods, using the beta value ( good but not perfect ), and using the Steinhart Hart algorythm ( perfect ).

Using beta values :

Set the beta value in the configuration file :

    temperature_control.hotend.beta       4066   # set beta for thermistor

#### About beta values

The betas published by most manufacturers, are for the 0-80°C range.

For the 185-230°C this results in readings being about 7-10°C too high.

This means beta values are usually good for a heated bed, but not for a hotend.

If the thermistor is 100K ohms at 25°C then this is usually enough.

You can also set r0,t0,r1,r2 but this is not usually needed as the defaults are sufficient.

If you do not know what thermistor model you have, contact the designer or seller of your 3D printer, hotend or heated bed and get the spec which will tell you what the beta value is for the thermistor.

Using the Steinhart Hart algorythm :

This is the recommended method. Set the parameters in the configuration file :

    temperature_control.hotend.coefficients 0.000722376862540841,0.000216302098124288,0.000000092640163984

To find what the Steinhart Hart coefficients for a given thermistor are, please read the SteinhartHart page.

If you have the temperature curve for your thermistor, you can also define three points on that curve, and let Smoothie calculate everything :

    temperature_control.hotend.rt_curve          20.0,126800,150,1360,240,206.5

#### Thermocouple

Thermocouples are currently supported by connecting a MAX31855 chip to one of the SPI channels. Thermocouples give a stable measurement over a wide temperature range, and can typically withstand higher temperatures than thermistors.

Note: Connecting a thermocouple and an LCD panel to the same SPI bus does not work.

Here is an example of how to connect the Adafruit Thermocouple Amplifier MAX31855 breakout board to the smoothieboard.

Smoothieboard | Breakout board
--- | ---
3v3 | Vin
GND | GND
SSEL1 | CS
MOSI1 | DO
SCK1 | CLK
MISO | Not used

To configure smoothie to use the thermocouple connected like this, replace the thermistor and thermistor_pin parameters with the following:

    temperature_control.hotend.sensor        max31855

The SPI channel and chip select pin can be changed using the following parameters:

    temperature_control.hotend.chip_select_pin    0.16
    temperature_control.hotend.spi_channel         0    # SPI channel 0 or 1

### Heating things up.

To reach the desired temperature, you need a means of changing the temperature. This is usually done by letting current through :

- Resistors
- Heater cartridges
- PCB, kapton or silicone heater plates

This is for example how hotends or heated beds are heated to their target temperature.

The component turning the current on and off can be a mosFET ( like on Smoothieboard ) or a Solid-State Relay for example.

This is controlled from the board running Smoothie using a GPIO pin. In the case of mosfets, on a [[Smoothieboard]], a given pin is connected to a given mosfet, and you have to use that specific pin to control that specific mosfet. See the schematic on the right or on the Smoothieboard page to see what mosfet corresponds to what pin.

    temperature_control.hotend.heater_pin        2.7

Note on pins :

You can have a pin's output inverted by adding a "!" after this pin's number in the config line, example :

    my_pin_name                      19!

There are other modifiers for pins :

#### All options

! | invert pin
o | set pin to open drain
^ | set pin to pull up
v | set pin to pull down
- | to set no pullup
@ | to set repeater mode

### Controlling with G-codes

By default, Smoothie will not heat anything. That could be a dangerous thing to do unwatched.

You have to send G-codes to turn your heater on and off, set a given temperature etc.

There is a set of widely used G-codes corresponding to different usual actions ( for example setting the hotend temperature is `M104` in the Reprap world ).

But as you are defining your own custom temperature controller, you have to choose what gcode will be used to control it, Smoothie doesn't know what exactly it's controlling.

So for example if this is a hotend, it will look something like this for the "standard" gcodes :

    temperature_control.hotend.set_m_code            104
    temperature_control.hotend.set_and_wait_m_code   109

set_m_code is used to set a given temperature, and continue running Smoothie immediately. set_and_wait_m_code is used to set a given temperature, and then pause Smoothie until that temperature is reached.

### Reading with G-code

There is a single g-code used to read temperature for all the temperature_control modules at the same time : `M105`

But it has to have a way to tell you what temperature corresponds to what specific module.

There is a standard format for this which was used before Smoothie and still is today :

    ok T:22.1 /0.0 @0 B:22.5 /75.0 @210

Here T is the hotend, and B is the bed. This is a convention. But in your configuration, we have to specify which is which :

    temperature_control.hotend.designator        T

### Bang Bang Control

The simplest form of heat control is called bang bang this simply turns the heater on or off depending on whether it is under or over the target temperature (plus some hysteresis).
This is best used for high amp beds using a relay to turn it on and off.

to enable this form of control in the config define the following…

    temperature_control.bed.bang_bang            true            # set to true to use bang bang control rather than PID
    temperature_control.bed.hysteresis            2.0              # set to the temperature in degrees C to use as hysteresis when

The default form of heater control is PID.

### PID

PID is important. Without PID, a simple way to control temperature would be :

- If temperature too cold, turn heater on
- If temperature too hot, turn heater off

But there is a big problem with that method. Due to temperature not travelling instantly in what you heat from the heater to the thermistor, when the thermistor reads a given temperature, the heater is already hotter than what the thermistor reads. And that overshooting is something we do not want. It means reaching temperatures that could be undesirable, and it means you will not be able to correctly stabilize the temperature.

The solution to this is PID. It uses some math, allowing us to correct those problems by turning the heater on and off in a smarter sequence.

The P, I and D factors are configured as follows :

    temperature_control.hotend.p_factor     100
    temperature_control.hotend.i_factor     0.1
    temperature_control.hotend.d_factor     100

But the really tricky thing is to find the right values for these 3 factors : the default ones are most probably wrong for your setup. So unless you have been given those values with your hardware, or you are a PID grand-master, you will need some help :

#### Marlin

Do not try to use PID settings from Marlin as they are not compatible.

### PID autotuning

Using a process of trial and error, and some magic math, Smoothie can automatically tune ( find ) your P, I and D factors.

Here is an example of the G-code command used to launch PID autotune :

    M303 E0 S190

- E0 is the number of the temperaturecontrol module, in the order they have been configured. Here it would be 0 for the hotend, and 1 for the bed.
- S190 is the temperature to autotune for. Use the temperature you will be using your heater at in real life. For a hotend here we use 190c

When you run the command, tuning begins :

    Target: 190.0
    Start PID tune, command is M303 E0 S190
    T: Starting PID Autotune, M304 aborts
    ok
    T:  21.3/190.0 @80 1 0/8
    T:  22.0/190.0 @80 1 0/8
    T:  22.3/190.0 @80 1 0/8
    T:  22.1/190.0 @80 1 0/8
    Etc ...

It continues for a while ( 8 loops ), heating up, cooling down, and trying new values. Then :

    T: 190.4/190.0 @0 0 7/8
    T: 190.2/190.0 @0 0 7/8
    Cycle 7:
    Max: 190.8  Min: 184.3  high time: 48.2s  low time: 7.5s
    Averages over last 3 cycles: Max:  81.8c  Min:  79.0c  high samples: 408  low samples:  68
      ku: 17.7607
      tu: 23.7929
    Trying:
      Kp:  10.7
      Ki: 0.045
      Kd:    32
    PID Autotune Complete! The settings above have been loaded into memory, but not written to your config file.

Now edit your configuration to use those three values ( Kp is `p_factor`, Ki is `i_factor`, Kd is `d_factor` ), reset, and temperature control should work much better.

Alternatively, you can also enter the following G-code :

    M500

Which will save the configuration values automatically in a configuration override file.

Learn more about configuration overrides here : Configuring Smoothie

### Fine Tuning

#### If you are using a 12v heater on a 24v system

you need to set max_pwm to 64

    temperature_control.hotend.max_pwm 64 #

#### If you are getting 10°C or more initial overshoot of temperature

oyou can set i_max to a lower value (default is max_pwm)
128 seems to be a good value, but it can be tuned with `M303 S0 Xnnn` where nnn is a number <= max_pwm

    temperature_control.hotend.i_max 128 #

#### To avoid accidental setting of too high a temperature

you can set max_temp to the maximum temp that is safe for the target heater.
This will ignore any setting of temperatures that are higher than this and instead set the temp to max_temp.

    temperature_control.hotend.max_temp 230    #

## Supported G-codes

- `M105` : Configurable, Get the current temperature
- `M104` : Configurable, Set the temperature target for this module. 104 is usually used for hotends, the S parameter is the temperature in °C, for example : `M104 S150`
- `M109` : Configurable, Set the temperature target for this module then wait for it to be reached. 109 is usually used for hotends, the S parameter is the temperature in °C, for example : `M109 S150`
- `M140` : Configurable, Set the temperature target for this module. 140 is usually used for heated beds, the S parameter is the temperature in °C, for example : `M140 S50`
- `M190` : Configurable, Set the temperature target for this module then wait for it to be reached. 190 is usually used for heated beds, the S parameter is the temperature in °C, for example : `M190 S50`
- `M301` : Edit temperature control PID parameters, for example : `M301 S0 P30 I10 D10`, where S is the number of the temperaturecontrol module to change the parameters for
- `M305` : Set parameters for the thermistor, where `B` is beta, `R` is r0 and `X` is t0.
- `M500` : Save volatile settings to an override file
- `M503` : Display overridden settings if any

Note : the lines above that say configurable mean that this is the usual command format, but that you can change the number in the configuration file.

page revision: 48, last edited: 14 Jan 2015 16:22
Edit Tags History Files Print Site tools + Options
Edit Sections Append Edit Meta Watchers Backlinks Page Source Parent Lock Page Rename Delete
Click here to edit contents of this page.
Click here to toggle editing of individual sections of the page (if possible). Watch headings for an "edit" link when available.
Append content without editing the whole page source.
Check out how this page has evolved in the past.
If you want to discuss contents of this page - this is the easiest way to do it.
View and manage file attachments for this page.
A few useful tools to manage this Site.
See pages that link to and include this page.
Change the name (also URL address, possibly the category) of the page.
View wiki source for this page without editing.
View/set parent page (used for creating breadcrumbs and structured layout).
Notify administrators if there is objectionable content in this page.
Something does not work as expected? Find out what you can do.
General Wikidot.com documentation and help section.
Wikidot.com Terms of Service - what you can, what you should not etc.
Wikidot.com Privacy Policy.